package com.itsq.service.resources.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.mapper.ChangeMapper;
import com.itsq.pojo.dto.PageCarDto;
import com.itsq.pojo.entity.Banner;
import com.itsq.pojo.entity.Car;
import com.itsq.mapper.CarMapper;
import com.itsq.pojo.entity.Change;
import com.itsq.service.resources.CarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-01-29
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired(required = false)
    private CarMapper carMapper;
    @Autowired(required = false)
    private ChangeMapper changeMapper;

    @Override
    public Page<Car> selCarPageFront(PageCarDto pageCarDto) {
        Page<Car> page=new Page<>(pageCarDto.getPageIndex(),pageCarDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageCarDto.getOrderByField()!=null&&pageCarDto.getCarNumber()==null){
            queryWrapper.orderByDesc(pageCarDto.getOrderByField());
        }else if(pageCarDto.getCarNumber()!=null){
            queryWrapper.orderByDesc(pageCarDto.getOrderByField());
            queryWrapper.like("car_number",pageCarDto.getCarNumber());
        }else{
            queryWrapper.orderByDesc("cre_date");
        }
        return carMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<Car> selCarPageAfter(PageCarDto pageCarDto) {
        Page<Car> page=new Page<>(pageCarDto.getPageIndex(),pageCarDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
         if(pageCarDto.getCarNumber()!=null){
            queryWrapper.eq("car_number",pageCarDto.getCarNumber());
        }else{
            queryWrapper.orderByDesc("cre_date");
        }
        return carMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    public Integer insert(Car car) {
        int i = carMapper.insert(car);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }else if(car.getCompanyId()!=null||car.getDepartmentId()!=null){
                Change change=new Change();
                change.setCarId(car.getId());
                change.setCompanyId(car.getCompanyId());
                change.setDepartmentId(car.getDepartmentId());
                //设置日期格式
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                change.setCreDate(new Date());
                int insert = changeMapper.insert(change);
                if(insert<1){
                    throw new APIException(ErrorEnum.is_ISNULL);
                }
        }
        return i;
    }

    @Override
    @Transactional
    public Integer update(Car car) {
        int i = carMapper.updateById(car);
        if(i<1){
            throw new APIException(ErrorEnum.is_UPDATE);
        }else if(car.getCompanyId()!=null||car.getDepartmentId()!=null){
            Change change=new Change();
            change.setCarId(car.getId());
            change.setCompanyId(car.getCompanyId());
            change.setDepartmentId(car.getDepartmentId());
            //设置日期格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            change.setCreDate(new Date());
            int insert = changeMapper.insert(change);
            if(insert<1){
                throw new APIException(ErrorEnum.is_ISNULL);
            }
        }
        return i;
    }

    @Override
    public Car selCarById(Integer id) {
        return carMapper.selectById(id);
    }

    @Override
    public List<Car> selCarList() {
        QueryWrapper queryWrapper=new QueryWrapper();
        return carMapper.selectList(queryWrapper);
    }

    @Override
    public List<Car> selCarListByCompanyId(Integer companyId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("department_id",companyId);
        return carMapper.selectList(queryWrapper);
    }

    @Override
    public Integer delete(Integer id) {
        return carMapper.deleteById(id);
    }


}
